#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <libtiffanygirl/libtiffanygirl.h>
#include "systray_menu.h"
#include "note.h"
#include "tiffanygirl.h"

extern TiffanyGirl *tiffgirl;

static void systray_menu_item_activate(GtkWidget *widget, TGirlNote *tgnote)
{
	GList *list;
	TiffanyGirlNote *tiffgnote;

	for (list=tiffgirl->windowlist;list;list=g_list_next(list)) {
		tiffgnote = (TiffanyGirlNote *)list->data;

		/* find the note which is opened already */
		if (tgnote==tiffgnote->tgnote) {
			gtk_window_present(tiffgnote->window->window);
			return;
		}
	}

	/* add to window list */
	tiffgnote = (TiffanyGirlNote *)g_new0(TiffanyGirlNote, 1);
	tiffgnote->tgnote = tgnote;
	tiffgnote->window = note_dialog_new(tgnote);
	tiffgirl->windowlist = g_list_append(tiffgirl->windowlist, tiffgnote);
}

SysTrayMenu *systray_menu_new(GList *notes)
{
	SysTrayMenu *stmenu;
	SysTrayMenuItem *stitem;
	GtkWidget *item;
	GList *list;
	TGirlNote *tgnote;
	gint i = 10;

	stmenu = g_new0(SysTrayMenu, 1);
	stmenu->menu = gtk_menu_new();
	stmenu->items = NULL;

	/* Add new note */
	item = gtk_image_menu_item_new_with_mnemonic(_("Create a new Note"));
	gtk_menu_append(GTK_MENU(stmenu->menu), item);

	/* Separator */
	item = gtk_separator_menu_item_new();
	gtk_menu_append(GTK_MENU(stmenu->menu), item);

	/* note list */
	for (list=notes;(list) ? i : NULL;list=g_list_next(list), --i) {
		tgnote = (TGirlNote *)list->data;

		/* menu item */
		stitem = (SysTrayMenuItem *)g_new0(SysTrayMenuItem, 1);
		stitem->tgnote = tgnote;
		stitem->widget = gtk_image_menu_item_new_with_mnemonic(tgnote->title);
		g_signal_connect(G_OBJECT(stitem->widget), "activate", G_CALLBACK(systray_menu_item_activate), stitem->tgnote);
		g_object_weak_ref(stitem->widget, g_free, stitem);

		gtk_menu_append(GTK_MENU(stmenu->menu), stitem->widget);

		stmenu->items = g_list_append(stmenu->items, stitem);
	}

	gtk_widget_show_all(stmenu->menu);

	return stmenu;
}
